﻿using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using ReferenceTemplate.Models;
using ReferenceTemplate.Services;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ReferenceTemplate.ViewModels
{
    public class MainViewModel : ViewModelBase
    {
        private ObservableCollection<Link> links;

        public ObservableCollection<Link> Links
        {
            get { return links; }
            set { links = value; RaisePropertyChanged(() => Links); }
        }

        private Link selectedLink;

        public Link SelectedLink
        {
            get { return selectedLink; }
            set { selectedLink = value; RaisePropertyChanged(() => SelectedLink); CanPinToStart = !tileService.IsPinnedToStart(SelectedLink); RaisePropertyChanged(() => Links); }
        }

        private string appName;

        public string AppName
        {
            get { return appName; }
            set { appName = value; RaisePropertyChanged(() => AppName); }
        }

        private string background;

        public string Background
        {
            get { return background; }
            set { background = value; RaisePropertyChanged(() => Background); }
        }

        private bool canPinToStart = true;

        public bool CanPinToStart
        {
            get { return canPinToStart; }
            set { canPinToStart = value; RaisePropertyChanged(() => CanPinToStart); }
        }

        private string color;

        public string Color
        {
            get { return color; }
            set { color = value; RaisePropertyChanged(() => Color); }
        }

        private RelayCommand pinToStart;

        public RelayCommand PinToStart
        {
            get { return pinToStart; }
            set { pinToStart = value; }
        }

        private RelayCommand copyLink;

        public RelayCommand CopyLink
        {
            get { return copyLink; }
            set { copyLink = value; }
        }

        private RelayCommand openLink;

        public RelayCommand OpenLink
        {
            get { return openLink; }
            set { openLink = value; }
        }

        IDataService dataService;
        ITileService tileService;
        ILinksService linkService;

        public MainViewModel(IDataService _dataService, ITileService _tileService, ILinksService _linkService)
        {
            dataService = _dataService;
            tileService = _tileService;
            linkService = _linkService;

            Links = new ObservableCollection<Link>();

            LoadData();

            PinToStart = new RelayCommand(PinToStartAction);
            OpenLink = new RelayCommand(OpenLinkAction);
            CopyLink = new RelayCommand(CopyLinkAction);
        
        }

        private async void LoadData()
        {
            AppData data = await dataService.LoadData();

            Links = new ObservableCollection<Link>(data.Links);
            Color = data.Color;
            Background = data.BackgroundImage;
            AppName = data.AppName;
        }

        private void PinToStartAction()
        {
            tileService.PintToStart(SelectedLink);
            CanPinToStart = false;
        }

        private void OpenLinkAction()
        {
            linkService.OpenLink(SelectedLink);
        }

        private void CopyLinkAction()
        {
            linkService.CopyLink(SelectedLink);
        }
    }
}
